#include "QQSettings.h"

QQSettings::QQSettings(QObject* parent)
	: QObject(parent)
{
	mUserInfo = nullptr;
}

QQSettings::~QQSettings()
{
}

QQSettings* QQSettings::getInstance()
{
	static QQSettings* instance = nullptr;
	if (instance == nullptr)
		instance = new QQSettings(qApp);
	return instance;
}

void QQSettings::init()
{
}

void QQSettings::destroy()
{
	QSharedMemory sharedMemory("unique_key_for_shared_memory_" + getUserInfo()->account);

	if (sharedMemory.isAttached())
	{
		sharedMemory.lock();
		bool* flag = static_cast<bool*>(sharedMemory.data());
		*flag = false;
		sharedMemory.unlock();
		if (!sharedMemory.detach())
		{
			qCritical() << sharedMemory.key() + " " + sharedMemory.errorString();
		}
	}
}

QString QQSettings::userDataDirectory()
{
	static QString dir = "";
	if (dir.isEmpty()) {
		dir = qApp->applicationDirPath() + "/data/";
		if (!QDir(dir).exists()) {
			QDir().mkpath(dir);
		}
	}
	return dir;
}

QString QQSettings::userDownloadDirectory()
{
	static QString dir = QStandardPaths::writableLocation(QStandardPaths::DownloadLocation);
	if (!QDir(dir).exists()) {
		QDir().mkpath(dir);
	}
	return dir;
}

QString QQSettings::userImageDirectory()
{
	static QString dir = "";
	if (dir.isEmpty()) {
		dir = userDataDirectory() + "/image/";
		if (!QDir(dir).exists()) {
			QDir().mkpath(dir);
		}
	}
	return dir;
}

QString QQSettings::userVideoDirectory()
{
	static QString dir = "";
	if (dir.isEmpty()) {
		dir = userDataDirectory() + "/video/";
		if (!QDir(dir).exists()) {
			QDir().mkpath(dir);
		}
	}
	return dir;
}

QString QQSettings::userAudioDirectory()
{
	static QString dir = "";
	if (dir.isEmpty()) {
		dir = userDataDirectory() + "/audio/";
		if (!QDir(dir).exists()) {
			QDir().mkpath(dir);
		}
	}
	return dir;
}

QString QQSettings::userAnimationDirectory()
{
	static QString dir = "";
	if (dir.isEmpty()) {
		dir = userDataDirectory() + "/animation/";
		if (!QDir(dir).exists()) {
			QDir().mkpath(dir);
		}
	}
	return dir;
}

QString QQSettings::userLogDirectory()
{
	static QString dir = "";
	if (dir.isEmpty()) {
		dir = userDataDirectory() + "/log/";
		if (!QDir(dir).exists()) {
			QDir().mkpath(dir);
		}
	}
	return dir;
}

QString QQSettings::userLogZipDirectory()
{
	static QString dir = "";
	if (dir.isEmpty()) {
		dir = userLogDirectory() + "/log_zip/";
		if (!QDir(dir).exists()) {
			QDir().mkpath(dir);
		}
	}
	return dir;
}

QString QQSettings::applicationResourcesDirectory()
{
	return qApp->applicationDirPath();
}

QString QQSettings::applicationDhapeLibraryDirectory()
{
	static QString dir = "";
	if (dir.isEmpty()) {
		dir = applicationResourcesDirectory() + "/shape/";
		if (!QDir(dir).exists()) {
			QDir().mkpath(dir);
		}
	}
	return dir;
}

QString QQSettings::applicationImageLibraryDirectory()
{
	static QString dir = "";
	if (dir.isEmpty()) {
		dir = applicationResourcesDirectory() + "/image/";
		if (!QDir(dir).exists()) {
			QDir().mkpath(dir);
		}
	}
	return dir;
}

QString QQSettings::applicationImageLibraryDirectory(const QString& type)
{
	return QString();
}

QString QQSettings::applicationApplicationsLibraryDirectory()
{
	return QString();
}

QString QQSettings::applicationCustomizationDirectory()
{
	return QString();
}

QString QQSettings::applicationCustomFontDirectory()
{
	return QString();
}

QString QQSettings::applicationAudiosLibraryDirectory()
{
	static QString dir = "";
	if (dir.isEmpty()) {
		dir = applicationResourcesDirectory() + "/audio/";
		if (!QDir(dir).exists()) {
			QDir().mkpath(dir);
		}
	}
	return dir;
}

QString QQSettings::applicationVideosLibraryDirectory()
{
	return QString();
}

QString QQSettings::applicationAnimationsLibraryDirectory()
{
	return QString();
}

QString QQSettings::applicationStartupHintsDirectory()
{
	return QString();
}

QString QQSettings::getQRCodeEntryBaseUrl() const
{
	return QString("https://gitee.com/datashowagent/IMQQ?time=");
}

void QQSettings::setCurrentAccountEntryState(bool state)
{
	QSharedMemory sharedMemory("unique_key_for_shared_memory_" + getUserInfo()->account);
	if (!sharedMemory.isAttached())
		if (!sharedMemory.attach())
		{
			qCritical() << sharedMemory.key() + " " + sharedMemory.errorString();
			return;
		}
	if (sharedMemory.size() == 0)
	{
		sharedMemory.create(sizeof(bool));
		qWarning() << sharedMemory.key() + " " + sharedMemory.errorString();
		sharedMemory.lock();
		bool* flag = static_cast<bool*>(sharedMemory.data());
		*flag = false;
		sharedMemory.unlock();
		if (!sharedMemory.attach())
		{
			qCritical() << sharedMemory.key() + " " + sharedMemory.errorString();
			return;
		}
	}
	sharedMemory.lock();
	bool* flag = static_cast<bool*>(sharedMemory.data());
	*flag = state;
	sharedMemory.unlock();
}

bool QQSettings::checkCurrentAccountEntryState(const QString& account)
{
	QSharedMemory sharedMemory("unique_key_for_shared_memory_" + account);

	if (!sharedMemory.isAttached())
		if (!sharedMemory.attach())
		{
			qCritical() << sharedMemory.key() + " " + sharedMemory.errorString();
			return false;
		}

	if (sharedMemory.size() == 0)
	{
		sharedMemory.create(sizeof(bool));
		qWarning() << sharedMemory.key() + " " + sharedMemory.errorString();
		sharedMemory.lock();
		bool* flag = static_cast<bool*>(sharedMemory.data());
		*flag = false;
		sharedMemory.unlock();
		if (!sharedMemory.attach())
		{
			qCritical() << sharedMemory.key() + " " + sharedMemory.errorString();
			return false;
		}
	}

	sharedMemory.lock();
	bool* flag = static_cast<bool*>(sharedMemory.data());
	bool result = *flag;
	sharedMemory.unlock();
	return result;
}

void QQSettings::setUserInfo(QQUser* user)
{
	mUserInfo = user;
}

QQUser* QQSettings::getUserInfo() const
{
	return mUserInfo;
}

qint64 QQSettings::getPictureMaxSize() const
{
	return mPictureMaxSize;
}

qint64 QQSettings::getPictureMaxCount() const
{
	return mPictureMaxCount;
}

QSize QQSettings::getPictureScreenMaxSize() const
{
	return mPictureScreenMaxSize;
}

qint64 QQSettings::getFileMaxSize() const
{
	return mFileMaxSize;
}

qint64 QQSettings::getFileMaxCount() const
{
	return mFileMaxCount;
}

qint64 QQSettings::getFilePieceSize() const
{
	return mFilePieceSize;
}
